import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

browser = webdriver.Chrome()
try:
    browser.get('https://www.baidu.com')
    input = browser.find_element(By.ID,'kw')
    # find_element_by_id 是旧方法 新方法 find_element_by_id
    input.send_keys('Python')
    input.send_keys(Keys.ENTER)
    wait = WebDriverWait(browser, 10)
    wait.until(EC.presence_of_element_located((By.ID, 'content_left')))
    print(browser.current_url)
    print(browser.get_cookies())
    print(browser.page_source)
finally:
    browser.close()

'''
By.ID: 通过 ID 定位元素
By.NAME: 通过 name 属性定位元素
By.CLASS_NAME: 通过类名定位元素
By.XPATH: 通过 XPath 定位元素
By.CSS_SELECTOR: 通过 CSS 选择器定位元素
'''
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
# input_first = browser.find_element_by_id('q')
# input_second = browser.find_element_by_css_selector('#q')
# input_third = browser.find_element_by_xpath('//*[@id="q"]')
input_first = browser.find_element(By.ID,'q')
input_second = browser.find_element(By.CSS_SELECTOR,'#q')
lis = browser.find_element(By.CSS_SELECTOR,'search-suggest-combobox')
input_third = browser.find_element(By.XPATH,'//*[@id="q"]')
print(input_first, input_second, input_third)
browser.close()

browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input = browser.find_element(By.ID,'q')
input.send_keys('iPhone')
time.sleep(1)
input.clear()
input.send_keys('iPad')
button = browser.find_element(By.CSS_SELECTOR,'btn-search')
button.click()